package com.yunupmall.service.admin.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yunupmall.api.fordVO.LogisticsVo;
import com.yunupmall.api.fordentity.Logistics;
import com.yunupmall.mapper.commoditymapper.LogisticsMapper;
import com.yunupmall.service.admin.service.LogisticsService;
import com.yunupmall.uits.IsMobileUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class Logisticsimpl extends ServiceImpl<LogisticsMapper, Logistics> implements LogisticsService {
    @Override
    public String saveData(LogisticsVo logisticsVo) {
        if (!IsMobileUtils.isMobile(logisticsVo.getPhone())) {
            if (!IsMobileUtils.isPhone(logisticsVo.getPhone())){
                throw  new RuntimeException("联系电话校验不通过");
            }
        }
        Logistics logistics = new Logistics();
        BeanUtils.copyProperties(logisticsVo, logistics);
        logistics.setDeflag(0);
        logistics.setCeateTime(LocalDateTime.now());
        if (this.save(logistics)){
            return "成功";
        }else {
            throw new RuntimeException("失败");
        }
    }

    @Override
    public Page<Logistics> pageDate(LogisticsVo logisticsVo) {
        Page<Logistics> logisticsPage = new Page<>(logisticsVo.getPage(), logisticsVo.getLimit());
        LambdaQueryWrapper<Logistics> logisticsLambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (ObjectUtils.isNotEmpty(logisticsVo.getName())){
            logisticsLambdaQueryWrapper.like(Logistics::getName,logisticsVo.getName());
        }
        logisticsLambdaQueryWrapper.eq(Logistics::getDeflag,0);
        Page<Logistics> page = this.page(logisticsPage, logisticsLambdaQueryWrapper);
        return page;
    }

    @Override
    public String del(Integer id) {
        LambdaQueryWrapper<Logistics> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Logistics::getId,id);
        Logistics logistics = new Logistics();
        logistics.setDeflag(1);
        if (this.update(logistics,wrapper)) {
            return "成功";
        }else {
            throw new RuntimeException("失败");
        }
    }

    @Override
    public List<Logistics> getList() {
        LambdaQueryWrapper<Logistics> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Logistics ::getDeflag,0);
        List<Logistics> list = this.list(wrapper);
        return list;
    }
}
